import {getReports} from "@/backend/services/secu-service";
import styles from "./page.module.scss"
import {uniqueSimilarArray} from "@/lib/utils";

const reportRow = (report: any) => {
    const tokens = report.tokens || [];
    let hitTokens = tokens.filter((t: string) => report.title.toUpperCase().includes(t.toUpperCase()))
    hitTokens = uniqueSimilarArray(hitTokens)
    const tagStr = hitTokens.join('；');
    if (!tagStr) return null;
    return <div key={report.report_id} className={styles.reportRow}>
        <p style={{color: 'yellow'}}><span style={{color: 'cyan'}}> {tagStr} </span> {report.title} </p>
        {report.highlight_abstract.map((a: string, id: number) => {
            const sentence = a.replace('<span class="keyword">', '<span class="keyword" style="color: magenta">')
            return <p key={id}>{id + 1}. <span dangerouslySetInnerHTML={{__html: sentence}}/></p>
        })}
    </div>
}


export default async function reportPage({params}: { params: Promise<{ code: string }> }) {
    const {code} = await params;
    const reports: any = await getReports(code);
    return <div>
        {reports.map((r: any) => reportRow(r))}
    </div>
}